package cn.mes.module.mes.controller.admin.basemachineinfo;

import cn.mes.module.mes.dal.dataobject.basemachineinfo.BaseMachinePeopleInfoDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.mes.framework.common.pojo.PageParam;
import cn.mes.framework.common.pojo.PageResult;
import cn.mes.framework.common.pojo.CommonResult;
import cn.mes.framework.common.util.object.BeanUtils;
import static cn.mes.framework.common.pojo.CommonResult.success;

import cn.mes.framework.excel.core.util.ExcelUtils;

import cn.mes.framework.apilog.core.annotation.ApiAccessLog;
import static cn.mes.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.mes.module.mes.controller.admin.basemachineinfo.vo.*;
import cn.mes.module.mes.dal.dataobject.basemachineinfo.BaseMachineInfoDO;
import cn.mes.module.mes.dal.dataobject.basemachineinfo.BaseDeviceDataDO;
import cn.mes.module.mes.service.basemachineinfo.BaseMachineInfoService;

@Tag(name = "管理后台 - 机台产线基础信息")
@RestController
@RequestMapping("/mes/base-machine-info")
@Validated
public class BaseMachineInfoController {

    @Resource
    private BaseMachineInfoService baseMachineInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建机台产线基础信息")
    @PreAuthorize("@ss.hasPermission('mes:base-machine-info:create')")
    public CommonResult<String> createBaseMachineInfo(@Valid @RequestBody BaseMachineInfoSaveReqVO createReqVO) {
        return success(baseMachineInfoService.createBaseMachineInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新机台产线基础信息")
    @PreAuthorize("@ss.hasPermission('mes:base-machine-info:update')")
    public CommonResult<Boolean> updateBaseMachineInfo(@Valid @RequestBody BaseMachineInfoSaveReqVO updateReqVO) {
        baseMachineInfoService.updateBaseMachineInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除机台产线基础信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:base-machine-info:delete')")
    public CommonResult<Boolean> deleteBaseMachineInfo(@RequestParam("id") String id) {
        baseMachineInfoService.deleteBaseMachineInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得机台产线基础信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:base-machine-info:query')")
    public CommonResult<BaseMachineInfoRespVO> getBaseMachineInfo(@RequestParam("id") String id) {
        BaseMachineInfoDO baseMachineInfo = baseMachineInfoService.getBaseMachineInfo(id);
        return success(BeanUtils.toBean(baseMachineInfo, BaseMachineInfoRespVO.class));
    }

    @GetMapping("/list-all-simple")
    @Operation(summary = "获得所有机台基础信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:base-machine-info:query')")
    public CommonResult<List<BaseMachineInfoRespVO>> getAlMachineList() {
        List<BaseMachineInfoDO> baseMachineInfoDOList = baseMachineInfoService.getBaseMachineInfoList();
        return success(BeanUtils.toBean(baseMachineInfoDOList, BaseMachineInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得机台产线基础信息分页")
    @PreAuthorize("@ss.hasPermission('mes:base-machine-info:query')")
    public CommonResult<PageResult<BaseMachineInfoDO>> getBaseMachineInfoPage(@Valid BaseMachineInfoPageReqVO pageReqVO) {
        PageResult<BaseMachineInfoDO> pageResult = baseMachineInfoService.getBaseMachineInfoPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出机台产线基础信息 Excel")
    @PreAuthorize("@ss.hasPermission('mes:base-machine-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBaseMachineInfoExcel(@Valid BaseMachineInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BaseMachineInfoDO> list = baseMachineInfoService.getBaseMachineInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "机台产线基础信息.xls", "数据", BaseMachineInfoRespVO.class,
                        BeanUtils.toBean(list, BaseMachineInfoRespVO.class));
    }

    // ==================== 子表（设备数据） ====================

    @GetMapping("/base-device-data/page")
    @Operation(summary = "获得设备数据分页")
    @Parameter(name = "deviceId", description = "设备ID")
    @PreAuthorize("@ss.hasPermission('mes:base-machine-info:query')")
    public CommonResult<PageResult<BaseDeviceDataDO>> getBaseDeviceDataPage(PageParam pageReqVO,
                                                                                        @RequestParam("deviceId") String deviceId) {
        return success(baseMachineInfoService.getBaseDeviceDataPage(pageReqVO, deviceId));
    }

    @PostMapping("/base-device-data/create")
    @Operation(summary = "创建设备数据")
    @PreAuthorize("@ss.hasPermission('mes:base-machine-info:create')")
    public CommonResult<String> createBaseDeviceData(@Valid @RequestBody BaseDeviceDataDO baseDeviceData) {
        return success(baseMachineInfoService.createBaseDeviceData(baseDeviceData));
    }

    @PutMapping("/base-device-data/update")
    @Operation(summary = "更新设备数据")
    @PreAuthorize("@ss.hasPermission('mes:base-machine-info:update')")
    public CommonResult<Boolean> updateBaseDeviceData(@Valid @RequestBody BaseDeviceDataDO baseDeviceData) {
        baseMachineInfoService.updateBaseDeviceData(baseDeviceData);
        return success(true);
    }

    @DeleteMapping("/base-device-data/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除设备数据")
    @PreAuthorize("@ss.hasPermission('mes:base-machine-info:delete')")
    public CommonResult<Boolean> deleteBaseDeviceData(@RequestParam("id") String id) {
        baseMachineInfoService.deleteBaseDeviceData(id);
        return success(true);
    }

	@GetMapping("/base-device-data/get")
	@Operation(summary = "获得设备数据")
	@Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:base-machine-info:query')")
	public CommonResult<BaseDeviceDataDO> getBaseDeviceData(@RequestParam("id") String id) {
	    return success(baseMachineInfoService.getBaseDeviceData(id));
	}

    // ==================== 子表（机台负责人员信息） ====================

    @GetMapping("/base-machine-people-info/page")
    @Operation(summary = "获得机台负责人员信息分页")
    @Parameter(name = "machineId", description = "机台信息ID")
    @PreAuthorize("@ss.hasPermission('mes:base-machine-info:query')")
    public CommonResult<PageResult<BaseMachinePeopleInfoDO>> getBaseMachinePeopleInfoPage(PageParam pageReqVO,
                                                                                          @RequestParam("machineId") String machineId) {
        return success(baseMachineInfoService.getBaseMachinePeopleInfoPage(pageReqVO, machineId));
    }

    @PostMapping("/base-machine-people-info/create")
    @Operation(summary = "创建机台负责人员信息")
    @PreAuthorize("@ss.hasPermission('mes:base-machine-info:create')")
    public CommonResult<String> createBaseMachinePeopleInfo(@Valid @RequestBody BaseMachinePeopleInfoDO baseMachinePeopleInfo) {
        return success(baseMachineInfoService.createBaseMachinePeopleInfo(baseMachinePeopleInfo));
    }

    @PutMapping("/base-machine-people-info/update")
    @Operation(summary = "更新机台负责人员信息")
    @PreAuthorize("@ss.hasPermission('mes:base-machine-info:update')")
    public CommonResult<Boolean> updateBaseMachinePeopleInfo(@Valid @RequestBody BaseMachinePeopleInfoDO baseMachinePeopleInfo) {
        baseMachineInfoService.updateBaseMachinePeopleInfo(baseMachinePeopleInfo);
        return success(true);
    }

    @DeleteMapping("/base-machine-people-info/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除机台负责人员信息")
    @PreAuthorize("@ss.hasPermission('mes:base-machine-info:delete')")
    public CommonResult<Boolean> deleteBaseMachinePeopleInfo(@RequestParam("id") String id) {
        baseMachineInfoService.deleteBaseMachinePeopleInfo(id);
        return success(true);
    }

    @GetMapping("/base-machine-people-info/get")
    @Operation(summary = "获得机台负责人员信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:base-machine-info:query')")
    public CommonResult<BaseMachinePeopleInfoDO> getBaseMachinePeopleInfo(@RequestParam("id") String id) {
        return success(baseMachineInfoService.getBaseMachinePeopleInfo(id));
    }

}